package model.factories.environment;

import java.util.Map;
import java.util.Scanner;
import java.util.regex.Pattern;
import model.Interpreter;
import model.SyntaxErrorException;
import model.commands.Command;
import model.commands.misc.LiteralCommand;
import model.factories.CommandFactory;
import util.Pixmap;


/**
 * A factory used to build SetBackgroundCommand objects.
 * 
 * @author Jimmy Longley
 */
public class SetShapeIndexCommandFactory extends CommandFactory {


    private final Pattern mySetShapeIndexPattern = Pattern.
            compile(getInterpreter().getLanguage().getString("SetShapeIndexPattern"));

    /**
     * 
     * @param interpreter an interpreter
     */
    public SetShapeIndexCommandFactory (Interpreter interpreter) {
        super(interpreter);
    }
    
    @Override
    public Command createCommand (Scanner commandReader,
                                  Map<String, Integer> variables) throws SyntaxErrorException {
        // remove command keyword
        commandReader.next();

        int shapeIndex = commandReader.nextInt();
        String filePath = commandReader.next();

        Pixmap shape = new Pixmap(filePath);
        getInterpreter().getEnvironment().setShape(shapeIndex, shape);

        // returns 1 on success
        return new LiteralCommand(1);

    }

    @Override
    public Pattern patternToMatch () {
        return mySetShapeIndexPattern;
    }
}
